package neusoft.edu.cn.guanshangshiguang.model;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.ListListener;
import neusoft.edu.cn.guanshangshiguang.bean.BookTypeBean;
import neusoft.edu.cn.guanshangshiguang.common.Common;
import neusoft.edu.cn.guanshangshiguang.service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BookTypeModel {

    private Retrofit retrofit;

    public BookTypeModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://"+ Common.IpAddress+"/literature_museum/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void getResultList(final ListListener ListListener){

        Service booktypeService = retrofit.create(Service.class);

        Call<List<BookTypeBean>> bookCall = booktypeService.getBookType();

        bookCall.enqueue(new Callback<List<BookTypeBean>>() {
                @Override
                public void onResponse(Call<List<BookTypeBean>> call, Response<List<BookTypeBean>> response) {
                    if(response!=null)
                        ListListener.onResponse(response.body());
                    else ListListener.onFail("获取图书分类列表失败");
                }

            @Override
            public void onFailure(Call<List<BookTypeBean>> call, Throwable t) {
                ListListener.onFail(t.toString());
            }
        });
    }
}
